//
//  OAuthVC.m
//  weiboObjectC
//
//  Created by 隗杰 on 16/2/21.
//  Copyright © 2016年 kkk. All rights reserved.
//

#import "OAuthVC.h"
#import <SVProgressHUD.h>
#import <AFNetworking.h>
@interface OAuthVC()<UIWebViewDelegate>

@property(strong,nonatomic)UIWebView *webView;

@end
@implementation OAuthVC


- (void)loadView {
    
    self.webView = [[UIWebView alloc] init];
    self.view = self.webView;
    
    self.webView.delegate = self;
//
}

- (void)viewDidLoad {
    // 关闭按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style: UIBarButtonItemStylePlain target:self action:@selector(closeClick)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc    ] initWithTitle:@"测试" style: UIBarButtonItemStylePlain target:self action:@selector(testButnClick)];
    
    // 调用方法加载授权页面
    [self loadOauthPage];
    
}

- (void)testButnClick {
    
    NSString * str = @"document.getElementById('userId').value = '18600678945' ,document.getElementById('passwd').value = 'weijie123'";
    
    [self.webView stringByEvaluatingJavaScriptFromString:str];
}

- (void)viewWillDisappear:(BOOL)animated {
    [SVProgressHUD dismiss];
}

- (void) loadOauthPage {
    
    NSString *URLString = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&&redirect_uri=%@",client_id,redirect_uri];
//    NSString *URLString = @"https://api.weibo.com/oauth2/authorize?client_id=3436874168&&redirect_uri=http://www.itcast.com";
    
    NSURLRequest *requset = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString]];
    
    [self.webView loadRequest:requset];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [SVProgressHUD show];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [SVProgressHUD dismiss];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    NSString *urlStr = request.URL.absoluteString;
    if (!urlStr) {
        return false;
    }
    if ([urlStr containsString:@"code"]) {
        
        NSString * query = request.URL.query;
        
        if (!query) {
            return false;
        }
        NSString *codeStr = @"code=";
        NSString *code = [query substringFromIndex:codeStr.length];
        NSLog(@"%@",code);
        [self loadAccessToken:code];
        return false;
    }
    
    return YES;
}


- (void)loadAccessToken:(NSString *)code {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    NSString *urlStr = @"https://api.weibo.com/oauth2/access_token";
    NSDictionary *parameters = @{@"client_id":client_id,@"client_secret":client_secret,@"grant_type":@"authorization_code",@"code":code,@"redirect_uri":redirect_uri};
//    AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
    [manager POST:urlStr parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}

- (void)closeClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
